

package com.kehutong.house.service;
import com.kehutong.common.DoveClient;
        import com.kehutong.common.urlconfig.FileUrlConfig;
        import com.kehutong.house.entity.Parking;
        import org.coraframework.inject.Inject;
        import org.coraframework.json.JSONArray;
        import org.coraframework.json.JSONObject;
        import org.coraframework.logger.Logger;
        import org.coraframework.logger.LoggerFactory;
        import org.coraframework.mvc.http.Bean;
        import org.coraframework.mvc.http.ReqMapping;
        import org.coraframework.mvc.http.result.Result;
        import org.coraframework.mvc.webservice.WebService;
        import org.coraframework.orm.jdbc.JdbcSession;
        import org.coraframework.orm.jdbc.Page;
        import org.coraframework.orm.jdbc.execute.entity.FindOne;
        import org.coraframework.orm.jdbc.execute.entity.FindPage;
        import org.coraframework.util.Objects;

        import java.util.List;

@WebService("/basic/parking/service/api")
public class ParkingServiceApi {

    static final Logger logger = LoggerFactory.getLogger(ParkingService.class);

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private DoveClient doveClient;


    @Inject
    private FileUrlConfig fileUrlConfig;

    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        FindPage<Parking> find = jdbcSession.findPage(Parking.class)
                .in(jsonObject, "id")
                .eq(jsonObject, "name")
                .eq(jsonObject,"communityName")
                .eq(jsonObject,"parkingClassifyNo").and()
                .eq("deleted", false);
        Page<Parking> page = find.page(jsonObject).exe();

        return page;
    }

    @ReqMapping("/get")
    public Object get(Parking parking) {
        return parking;
    }

    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(Parking parking) throws Exception {
        //车位是否已经存在
        FindOne<Parking> find = jdbcSession.findOne(Parking.class)
                .eq("deleted", false)
                .eq("communityNo", parking.getCommunityNo())
                .eq("classNo", parking.getClassNo())
                .eq("name", parking.getName());
        Parking item = find.exe();

        if (Objects.nonNull(item)) {
            return Result.error(0, "当前车位分类下已有相同的车位信息，请修改后重试");
        }
        jdbcSession.insert(parking);
        return Result.success(parking);
    }

    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(Parking parking) {
        //车位是否已经存在
        FindOne<Parking> find = jdbcSession.findOne(Parking.class)
                .eq("deleted", false)
                .eq("communityNo", parking.getCommunityNo())
                .eq("classNo", parking.getClassNo())
                .eq("name", parking.getName())
                .noEq("id", parking.getId());
        Parking item = find.exe();

        if (Objects.nonNull(item)) {
            return Result.error(0, "当前车位分类下已有相同的车位信息，请修改后重试");
        }
        jdbcSession.updateById(parking);
        return Result.success(parking);
    }

    @ReqMapping("/delete")
    public Object delete(Parking parking) throws Exception {
        JSONArray array = new JSONArray();
        JSONObject json = new JSONObject();
        json.put("id", parking.getOwnerNo());
        json.put("type", "1");
        json.put("houseNo", parking.getId());
        array.add(json);
        if (Objects.nonEmpty(array)) {
            doveClient.post("/crm/resident/service/batchDeleteResidentHouse", (http) -> {
                JSONObject body = new JSONObject();
                body.put("array", array);
                http.setBody(body.toJSONString());
            });
        }
        parking.setDeleted(true);
        jdbcSession.updateById(parking);

        return Result.success();
    }

    @ReqMapping("/batchUpdateParkingOwner")
    public Object batchUpdateParkingOwner(JSONObject jsonObject){
        List<Parking> list = jdbcSession.findArray(Parking.class)
                .eq(jsonObject,"ownerNo")
                .eq("deleted", false)
                .exe();

        list.forEach(item->{
            item.setOwnerNo(jsonObject.getString("ownerNo"));
            item.setOwnerName(jsonObject.getString("ownerName"));
            item.setOwnerMobile(jsonObject.getString("ownerMobile"));
        });

        jdbcSession.updateById(list);
        return Result.success();
    }
}
